package main

import (
	"encoding/json"
	"fmt"
	"github.com/tidwall/gjson"
	"go-gist/proto"
	"io/ioutil"
	"log"
	"net/http"
)

func init() {
	log.Println("init")
}

func main() {
	// 获取天气预测数据
	foreCastList := forecast()

	// 获取今日天气数据+今日空气质量数据
	todayWeather := todayWeather()

	// 获取生活方式数据
	lifeStyleList := lifestyle()

	weatherInfoRsp := weather.GetWeatherInfoRsp{Forecast: &foreCastList, TodayWeather: &todayWeather, LifeStyle: &lifeStyleList}

	fmt.Println(weatherInfoRsp)
}

func forecast() weather.ForeCastList {
	// 天气预测接口
	forecast := "https://free-api.heweather.net/s6/weather/forecast?location=贵阳&key=fd6bfd8fb9ef47be9d13fc9fa379aa59"
	forecastResult := httpGet(forecast)

	var foreCastDetailList []*weather.ForeCastDetail

	data := gjson.Get(forecastResult, "HeWeather6")
	if data.IsArray() {
		dailyForecast := data.Array()[0].Get("daily_forecast")
		dailyForecast.ForEach(func(key, value gjson.Result) bool {
			fcd := weather.ForeCastDetail{}
			_ = json.Unmarshal([]byte(value.String()), &fcd)

			foreCastDetailList = append(foreCastDetailList, &fcd)
			return true
		})
	}
	return weather.ForeCastList{ForecastDetail: foreCastDetailList}
}

func todayWeather() weather.TodayWeather {
	// 当前天气接口
	todayWeather := "https://free-api.heweather.net/s6/weather/now?location=贵阳&key=fd6bfd8fb9ef47be9d13fc9fa379aa59"
	//当前空气质量接口
	airNow := "https://free-api.heweather.net/s6/air/now?location=贵阳&key=fd6bfd8fb9ef47be9d13fc9fa379aa59"
	twResult := httpGet(todayWeather)
	airResult := httpGet(airNow)

	tw := weather.TodayWeather{}

	twResultData := gjson.Get(twResult, "HeWeather6")
	if twResultData.IsArray() {
		now := twResultData.Array()[0].Get("now")
		_ = json.Unmarshal([]byte(now.String()), &tw)
		//fmt.Println(tw)
	}

	airResultData := gjson.Get(airResult, "HeWeather6")
	if airResultData.IsArray() {
		now := airResultData.Array()[0].Get("air_now_city")
		_ = json.Unmarshal([]byte(now.String()), &tw)
		//fmt.Println(tw)
	}
	return tw
}

func lifestyle() weather.LifeStyleList {
	// 生活方式接口
	lifestyle := "https://free-api.heweather.net/s6/weather/lifestyle?location=贵阳&key=fd6bfd8fb9ef47be9d13fc9fa379aa59"

	var lifestyleTypeMap map[string]string
	lifestyleTypeMap = make(map[string]string)
	// comf：舒适度指数、cw：洗车指数、drsg：穿衣指数、flu：感冒指数、sport：运动指数、trav：旅游指数、uv：紫外线指数、
	// air：空气污染扩散条件指数、ac：空调开启指数、ag：过敏指数、gl：太阳镜指数、mu：化妆指数、
	// airc：晾晒指数、ptfc：交通指数、fsh：钓鱼指数、spi：防晒指数
	lifestyleTypeMap["comf"] = "舒适度指数"
	lifestyleTypeMap["cw"] = "洗车指数"
	lifestyleTypeMap["drsg"] = "穿衣指数"
	lifestyleTypeMap["flu"] = "感冒指数"
	lifestyleTypeMap["sport"] = "运动指数"
	lifestyleTypeMap["trav"] = "旅游指数"
	lifestyleTypeMap["uv"] = "紫外线指数"
	lifestyleTypeMap["air"] = "空气污染扩散条件指数"
	lifestyleTypeMap["ac"] = "空调开启指数"
	lifestyleTypeMap["ag"] = "过敏指数"
	lifestyleTypeMap["gl "] = "太阳镜指数"
	lifestyleTypeMap["mu"] = "化妆指数"
	lifestyleTypeMap["airc"] = "晾晒指数"
	lifestyleTypeMap["ptfc"] = "交通指数"
	lifestyleTypeMap["fsh"] = "钓鱼指数"
	lifestyleTypeMap["spi"] = "防晒指数"

	lifestyleResult := httpGet(lifestyle)

	var lifeStyleDetailList []*weather.LifeStyleDetail

	data := gjson.Get(lifestyleResult, "HeWeather6")
	if data.IsArray() {
		lifestyleList := data.Array()[0].Get("lifestyle")
		lifestyleList.ForEach(func(key, value gjson.Result) bool {
			lsd := weather.LifeStyleDetail{}
			_ = json.Unmarshal([]byte(value.String()), &lsd)
			lsd.Type = lifestyleTypeMap[lsd.Type]
			//fmt.Println(lsd)
			lifeStyleDetailList = append(lifeStyleDetailList, &lsd)
			return true
		})
	}
	return weather.LifeStyleList{LifestyleDetail: lifeStyleDetailList}
}

func httpGet(url string) string {
	resp, err := http.Get(url)
	if err != nil {
		panic(err)
	}
	defer resp.Body.Close()
	resultData, err := ioutil.ReadAll(resp.Body)
	return string(resultData)
}
